<?php
namespace Home\Controller;
use Think\Controller;
class ItemDetailController extends Controller {
	/*
	 * 获取商品详情
	 * @param itemid 
	 * return json
	 */
	public function index()
	{
		
		$itemid=I('request.itemid',0,'strip_tags');
		$item=M('item');
		$iteminfo=$item->where('itemid=%d',$itemid)
		->Field('itemid,itemname,price,itemimg,inventory,itemimg,bn')->select();
		          ////判断该商品是否存在多规格
		if(empty($iteminfo)){
			$result['status']=0;
			$result['msg']='暂无信息';
			echo json_encode($result,JSON_UNESCAPED_UNICODE);
			return '';
		}
		$productspc=M('product')->where("itemid='%d'",$itemid)->Field("productid,inventory,price,color")->select();
		$temp=array();
		
		$property=M('item_property a')->join('inner join '.C(DB_PREFIX).'type_propertyvalue b ON  a.propertyvalueid=b.valueid')
		         ->where('a.itemid=%d',$itemid)->getField('b.valueid,b.propertyvalue');
		$property=array_slice($property, 2); 
		$prostring=!empty($property)?'/'.$property[0].'/'.$property[1]:'';
		
		if(empty($productspc))
		{
			foreach($iteminfo as $k=>$v){
				$temp[0]['productid']=0;
				$temp[0]['selval']=$v['itemname'].$prostring;
				$temp[0]['price']=$v['price'];
				$temp[0]['inventory']=$v['inventory'];
			}

		}else{
			foreach ($productspc as $key => $value) {
				$temp[$key]['productid']=$value['productid'];
				$temp[$key]['selval']=$value['color'].$prostring;
				$temp[$key]['price']=$value['price'];
				$temp[$key]['inventory']=$value['inventory'];
			
			}
			
		}
		
			$result['status']=1;
			$result['iteminfo']=$iteminfo[0];

			$result['productspc']=$temp;
			echo json_encode($result,JSON_UNESCAPED_UNICODE);
	}

	//获取商品webview中的详情信息
	public function getItemHtml()
	{
		$itemid=I('get.itemid','','strip_tags');
		$item=M('item_desc');
		$temp=$item->where('itemid=%d',$itemid)->getField('itemdesc');
		if (!empty($temp))
		{
			echo "<style type='text/css'> p{margin-top:0px;margin-bottom:0px;}</style>".$temp;
		
		}else
		{
			$temp='暂无数据';
			echo $temp;
		}
	}
}